<?php
defined('IN_IXDCW') or exit('Access Denied');
$BRAND = array();
$do    = new AutoPictureModule($moduleid);
$arcid = isset($arcid) ? $arcid : 0;
$arbid = isset($arbid) ? $arbid : 0;
$armid = isset($armid) ? $armid : 0;
$alid  = isset($alid) ? $alid : 0;
$menus = array(
	array('添加' . $MOD['name'], '?moduleid=' . $moduleid . '&action=add'),
	array($MOD['name'] . '列表', '?moduleid=' . $moduleid),
);

if ($_catids) require DT_ROOT . '/admin/admin_check.inc.php';

if (in_array($action, array('', 'check', 'reject', 'recycle'))) {
	$sfields = array('模糊');
	$dfields = array('title');
	$sorder  = array('结果排序方式', '添加时间降序', '添加时间升序', '更新时间降序', '更新时间升序', '信息ID降序', '信息ID升序');
	$dorder  = array('listorder ASC', 'addtime DESC', 'addtime ASC', 'edittime DESC', 'edittime ASC', 'id DESC', 'id ASC');
	isset($fields) && isset($dfields[$fields]) or $fields = 0;
	isset($order) && isset($dorder[$order]) or $order = 0;
	$level = isset($level) ? intval($level) : 0;

	isset($datetype) && in_array($datetype, array('edittime', 'addtime')) or $datetype = 'addtime';

	$fields_select = dselect($sfields, 'fields', '', $fields);
	$level_select  = level_select('level', '级别', $level);
	$order_select  = dselect($sorder, 'order', '', $order);

	$condition = '';
	if ($arcid) $condition .= " And arcid={$arcid}";
	if ($arbid) $condition .= " And arbid={$arbid}";
	if ($armid) $condition .= " And amid={$armid}";
	if ($alid) $condition .= " And alid={$alid}";
	if ($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
	$timetype = strpos($dorder[$order], 'edit') === false ? 'add' : '';
}
switch ($action) {
	case 'add':
		if ($submit) {
			if ($do->pass($post)) {
				$do->setTable($post['arcid']);
				$do->add($post);
				Cookie::set('picture_upload_arids', "{$post['arbid']},{$post['arcid']},{$post['amid']},{$post['alid']},{$post['title']}");
				Prompt::message('添加成功！', Env::referer());
			} else {
				Prompt::message($do->getErrmsg());
			}
		}
		if (Cookie::has('picture_upload_arids')) {
			$arids = explode(',', Cookie::get('picture_upload_arids'));
			$arbid = $arids[0];
			$arcid = $arids[1];
			$amid  = $arids[2];
			$alid  = $arids[3];
			$title = $arids[4];
		}
		include tpl('edit', $module);
		break;
	case 'edit':
		$arcid or msg('请选择车系ID');
		$apid or msg();
		$do->setTable($arcid);
		$do->setApid($apid);
		$item = $do->getOne();
		$item or msg('不存在的车系图片!');
		if ($submit) {
			if (!$post['arcid']) msg('请选择车系');
			if ($post['arcid'] != $arcid) { // 信息换表
				$oldTable      = Table::autoPicture($arcid);
				$newTable      = Table::autoPicture($post['arcid']);
				$post          = $do->set($post);
				$post['armid'] = AutoRank::getArmidByArcid($post['arcid']);
				$sqlInsert     = Sql::insert($newTable, $post);
				if ($db->query(Sql::insert($newTable, $post))) {
					$db->query("delete from $oldTable where apid=$apid");
				}
			} else {
				$do->setTable($post['arcid']);
				if ($do->pass($post)) {
					$do->edit($post);
				}
			}

			dmsg('修改成功', $forward);
		} else {
			$item = $do->getOne();
			extract($item);
			$addtime = timetodate($addtime);
			$menuon  = array('4', '3', '2', '1');
			$menuid  = $menuon[$status];
			$tname   = '修改' . $MOD['name'];
			include tpl($action, $module);
		}
		break;
	case 'update':
		is_array($apid) or msg('请选择' . $MOD['name']);
		foreach ($apid as $v) {
			$do->update($v);
		}
		dmsg('更新成功', $forward);
		break;
	case 'delete':
		$apid or msg('请选择图片');
		$arcid or msg('请填写车系ID');
		$apid = is_array($apid) ? $apid : array($apid);
		$do->setTable($arcid);
		$do->delete($apid);
		dmsg('删除成功', $forward);
		break;
	case 'restore':
		$apid or msg('请选择' . $MOD['name']);
		$do->restore($apid);
		dmsg('还原成功', $forward);
		break;
	case 'clear':
		$do->clear();
		dmsg('清空成功', $forward);
		break;
	case 'level':
		$apid or msg('请选择' . $MOD['name']);
		$level = intval($level);
		$do->level($apid, $level);
		dmsg('级别设置成功', $forward);
		break;
	case 'recycle':
		$lists  = $do->getList('status=0' . $condition, $dorder[$order]);
		$menuid = 4;
		include tpl('index', $module);
		break;
	case 'reject':
		if ($apid && !$psize) {
			$do->reject($apid);
			dmsg('拒绝成功', $forward);
		} else {
			$lists  = $do->getList('status=1' . $condition, $dorder[$order]);
			$menuid = 3;
			include tpl('index', $module);
		}
		break;
	case 'check':
		if ($apid && !$psize) {
			$do->check($apid);
			dmsg('审核成功', $forward);
		} else {
			$lists  = $do->getList('status=2' . $condition, $dorder[$order]);
			$menuid = 2;
			include tpl('index', $module);
		}
		break;
	default:
		$do->setTable($arcid);
		$lists  = $do->getList(' 1 ' . $condition, $dorder[$order]);
		$menuid = 1;
		include tpl('index', $module);
		break;
}